package com.example.bankend.service.impl;

import com.example.bankend.entity.Card;
import com.example.bankend.entity.Saving;
import com.example.bankend.entity.Trade;
import com.example.bankend.entity.User;
import com.example.bankend.mapper.CardMapper;
import com.example.bankend.mapper.SavingMapper;
import com.example.bankend.mapper.TradeMapper;
import com.example.bankend.mapper.UserMapper;
import com.example.bankend.service.CardService;
import com.example.bankend.service.SavingService;
import com.example.bankend.service.TradeService;
import com.example.bankend.service.UserService;
import com.example.bankend.util.Random;
import com.example.bankend.vo.CardResult;
import com.example.bankend.vo.UserResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luolinila
 * @date 2025/6/18 09:45
 */
@Service
public class CardServerIpml implements CardService {
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SavingMapper savingMapper;
    @Autowired
    private TradeMapper tradeMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String createCard(Long adminID, String adminName, String adminPass, String pass, String savingName, String PID) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return "-1";
            }
            Saving saving = savingMapper.selectSavingByName(savingName);
            if (saving == null || "1".equals(saving.getIsDelete())) {
                return "10004";
            }
            User user = userMapper.selectByPID(PID);
            if (user == null || "1".equals(user.getIsDelete())) {
                return "10005";
            }
            List<String> list = selectAllCardID();
            String cardID = Random.randomCardNumber();
            while (list.contains(cardID)) {
                cardID = Random.randomCardNumber();
            }
            System.out.println(cardID);
            System.out.println(cardID.length());
            Card card = new Card();
            card.setCardID(cardID);
            card.setCurID("RMB");
            card.setOpenDate(new Timestamp(System.currentTimeMillis()));
            card.setOpenMoney(new BigDecimal(0));
            card.setBalance(new BigDecimal(0));
            card.setPass(pass);
            card.setIsReportLoss("0");
            card.setSavingID(saving.getSavingID());
            card.setCustomerID(user.getCustomerID());
            card.setIsDelete("0");
            cardMapper.insertCard(card);
            list.add(cardID);
            redisTemplate.opsForValue().set("allCardId", list, 1, TimeUnit.HOURS);
            return cardID;
        } catch (Exception e) {
            System.out.println(e);
            return "-1";
        }
    }

    @Override
    public Card getCardByID(String cardID) {
        try {
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete())) {
                return null;
            }
            return card;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public CardResult CardEncapsulaReturns(Card card) {
        try {
            String savingName = savingMapper.selectSavingById(card.getSavingID()).getSavingName();
            String userName = userMapper.selectByCunstomerID(card.getCustomerID()).getCustomerName();
            return new CardResult(card.getCardID(), card.getCurID(), card.getOpenDate(), card.getBalance(), savingName, userName, card.getIsReportLoss());
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public int deleteCard(Long adminID, String adminName, String adminPass, String cardID) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1;
            }
            Card card = getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete())) {
                return 10004;
            }
            tradeMapper.deleteallByCardID(cardID);
            cardMapper.deleteCard(cardID);
            return 200;
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }

    @Override
    public Long changeCardPass(Long adminID, String adminName, String adminPass, String cardID, String newPass) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1L;
            }
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete())) {
                return 10001L;
            }
            cardMapper.changeCardPass(cardID, newPass);
            return 200L;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }

    @Override
    public int handelLoss(String cardID, String pass) {
        try{
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete())) {
                return 10001;
            }
            if (!pass.equals(card.getPass())) {
                return 10003;
            }
            cardMapper.handelLoss(cardID);
            return 200;
        } catch (Exception e) {
            System.out.println(e);
            return -1;
        }
    }

    @Override
    public Long restoreUse(Long adminID, String adminName, String adminPass, String cardID) {
        try{
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1L;
            }
            Card card = cardMapper.getCardByID(cardID);
            if (card == null) {
                return 10001L;
            }
            User tureUser = userMapper.selectByCunstomerID(card.getCustomerID());
            if (tureUser == null) {
                return 10002L;
            }

            Long savingID = card.getSavingID();
            savingMapper.recoverSaving(savingID);
            Long customerID = card.getCustomerID();
            userMapper.recoverUserByID(customerID);
            cardMapper.restoreUse(cardID);
            tradeMapper.restoreAllByCardID(cardID);
            return 200L;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }

    @Override
    public List<String> selectAllCardID() {
        List<String> list = (List<String>) redisTemplate.opsForValue().get("allCardId");
        if (list == null) {
            list = cardMapper.selectAllCardID();
        }
        redisTemplate.opsForValue().set("allCardId", list, 1, TimeUnit.HOURS);
        return list;
    }

    @Override
    public List<Card> selectAllCard(Long adminID, String adminName, String adminPass) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return null;
            }
            return cardMapper.selectAllCard(adminID, adminName, adminPass);
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public List<Card> getAllCardByID(Long adminID, String adminName, String adminPass, Long customerID) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return null;
            }
            User user = userMapper.selectByCunstomerID(customerID);
            if (user == null || "1".equals(user.getIsDelete())) {
                return null;
            }
            return userMapper.getAllCardByID(user.getCustomerID());
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public String selecrBalance(String cardID, String pass) {
        Card card = cardMapper.getCardByID(cardID);
        if (card == null || "1".equals(card.getIsDelete()) || "1".equals(card.getIsReportLoss())) {
            return "10004";
        }
        if (!card.getPass().equals(pass)) {
            return "10005";
        }
        return card.getCardID();
    }

    @Override
    public String updatePass(String cardID, String lastPass, String newPass) {
        try{
            Card card = cardMapper.getCardByID(cardID);
            if (card == null || "1".equals(card.getIsDelete())) {
                return "10001";
            }
            if (!card.getPass().equals(lastPass)) {
                return "10002";
            }
            cardMapper.updatePass(cardID, newPass);
            return cardID;
        } catch (Exception e) {
            System.out.println(e);
            return "-1";
        }
    }
}
